package com.guli.gulimall.product.service.impl;

import com.guli.common.utils.R;
import com.guli.gulimall.product.dao.*;
import com.guli.gulimall.product.entity.*;
import com.guli.gulimall.product.feign.CouponFeignService;
import com.guli.gulimall.product.service.*;
import com.guli.gulimall.product.to.MemberPriceTo;
import com.guli.gulimall.product.to.SkuFullReductionTo;
import com.guli.gulimall.product.to.SkuLadderTo;
import com.guli.gulimall.product.to.SpuBoundsTo;
import com.guli.gulimall.product.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import org.springframework.transaction.annotation.Transactional;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @SuppressWarnings("all")
    @Autowired
    private SpuInfoDescDao spuInfoDescDao;

    @Autowired
    private SpuImagesService spuImagesService;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private CouponFeignService couponFeignService;
    @Override
    @Transactional
    public void addSpu(SpuInfoVo spuInfoVo){
        //-----------------pms数据库
        //1.pms_spu_info的基本信息
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuInfoVo,spuInfoEntity);
        //set两个时间
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        baseMapper.insert(spuInfoEntity);

        //2.pms_spu_desc 商品介绍图集表
        List<String> images = spuInfoVo.getDecript();
        if (images!=null&&images.size()>0){
            String imgs="";
            for (String url : images)
                imgs+=""+url;
                imgs=imgs.substring(1);

            SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
            descEntity.setSpuId(spuInfoEntity.getId());
            descEntity.setDecript(imgs);//商品介绍
            spuInfoDescDao.insert(descEntity);
        }

        //3.pms_spu_images 商品左上角小图
        List<String> images1 = spuInfoVo.getImages();
        if (images1!=null&&images1.size()>0){
            List<SpuImagesEntity> collect = images1.stream().map(url -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(spuInfoEntity.getId());
                spuImagesEntity.setImgUrl(url);
                return spuImagesEntity;

            }).collect(Collectors.toList());
            spuImagesService.saveBatch(collect);
        }

        //4.pms_product_attr_value 页面中添加的所有规格参数信息
        List<BaseAttrs> baseAttrs = spuInfoVo.getBaseAttrs();
        if (baseAttrs!=null){
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(spuInfoEntity.getId());
                productAttrValueEntity.setAttrId(attr.getAttrId());
                productAttrValueEntity.setAttrValue(attr.getAttrValues());
                //查询数据库得到的名称和状态
                AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                productAttrValueEntity.setQuickShow(attr.getShowDesc());
                productAttrValueEntity.setSearchType(attrEntity.getSearchType());

                return productAttrValueEntity;
            }).collect(Collectors.toList());
            //批量添加
            productAttrValueService.saveBatch(collect);
        }
        //9.sms_spu_bounds spu的积分表  todo feign调用

        if (spuInfoVo.getBounds()!=null){
            SpuBoundsTo spuBoundsTo = new SpuBoundsTo();
            spuBoundsTo.setSpuId(spuInfoEntity.getId());
            spuBoundsTo.setBuyBounds(spuInfoVo.getBounds().getBuyBounds());
            spuBoundsTo.setGrowBounds(spuInfoVo.getBounds().getGrowBounds());
            R r4=couponFeignService.saveSpuBounds(spuBoundsTo);
        }

        List<Skus> skus = spuInfoVo.getSkus();
        if(skus!=null){
            for(Skus sku : skus){
                // 5. pms_sku_info 商品sku的基本信息
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(sku,skuInfoEntity);
                skuInfoEntity.setSaleCount(0l);
                List<Images> images2 = sku.getImages();
                if(images2!=null){
                    //从集合中获取到多个
                    Images images3 = images2.stream().filter(item -> {
                        return item.getDefaultImg().equals(1);
                    }).findFirst().get();
                    //默认图
                    skuInfoEntity.setSkuDefaultImg(images3.getImgUrl());
                }
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setSkuDesc(spuInfoEntity.getSpuDescription());
                skuInfoDao.insert(skuInfoEntity);

                //6.pms_sku_images  商品sku的图片（一个sku多个图片）
                //非空判断（--图片可能为null-）
                List<Images> images3 = sku.getImages();
                if (images3!=null){
                    List<SkuImagesEntity> collect = images3.stream().filter(item -> {

                        return StringUtils.isNotEmpty(item.getImgUrl());
                    }).map(item -> {
                        SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                        skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                        skuImagesEntity.setDefaultImg(item.getDefaultImg());
                        skuImagesEntity.setImgUrl(item.getImgUrl());
                        return skuImagesEntity;
                    }).collect(Collectors.toList());
                    //批量添加
                    skuImagesService.saveBatch(collect);
                }

                //7..pms_sku_sale_attr_value   sku的销售属性表
                List<Attr> attrs = sku.getAttr();
                if (attrs!=null){
                    List<SkuSaleAttrValueEntity> collect = attrs.stream().map(attr -> {
                        SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                        BeanUtils.copyProperties(attr, skuSaleAttrValueEntity);
                        AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                        skuSaleAttrValueEntity.setSearchType(attrEntity.getSearchType());
                        skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());

                        return skuSaleAttrValueEntity;
                    }).collect(Collectors.toList());
                    skuSaleAttrValueService.saveBatch(collect);

                }
                //-------优惠卷数据----
                //8.sms_sku_ladder  sku的打折信息表
                SkuLadderTo skuLadderTo = new SkuLadderTo();
                skuLadderTo.setSkuId(skuInfoEntity.getSkuId());
                skuLadderTo.setAddOther(sku.getCountStatus());
                skuLadderTo.setFullCount(sku.getFullCount());
                skuLadderTo.setDiscount(sku.getDiscount());
                R r1=couponFeignService.saveSkuLadderTo(skuLadderTo);

                //10.sms_sku_full_reduction sku的满减
                SkuFullReductionTo skuFullReductionTo = new SkuFullReductionTo();
                skuFullReductionTo.setAddOther(sku.getPriceStatus());
                skuFullReductionTo.setFullPrice(sku.getFullPrice());
                skuFullReductionTo.setReducePrice(sku.getReducePrice());
                skuFullReductionTo.setSkuId(skuInfoEntity.getSkuId());
                R r2=couponFeignService.saveSkuFullReduction(skuFullReductionTo);

                //11.sms_member_price 会员等级价格表
                List<MemberPrice> memberPrices=sku.getMemberPrice();
                if (memberPrices!=null){
                    List<MemberPriceTo> collect = memberPrices.stream().map(item -> {
                        MemberPriceTo memberPriceTo = new MemberPriceTo();
                        memberPriceTo.setSkuId(skuInfoEntity.getSkuId());
                        memberPriceTo.setMemberLevelId(item.getId());
                        memberPriceTo.setMemberLevelName(item.getName());
                        memberPriceTo.setMemberPrice(item.getPrice());
                        return memberPriceTo;
                    }).collect(Collectors.toList());
                   R r3= couponFeignService.saveMemberPrice(collect);
                }



            }
        }


    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);

    }

}
